﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZZT
{
    sealed public class FlagCollection
    {
        static public readonly int FlagLength = 20;

        int count;
        Pointer pointer;

        internal FlagCollection(int count)
        {
            this.count = count;
            this.pointer = new Pointer((FlagLength + 1) * count);
        }

        internal FlagCollection(int count, byte[] memory)
        {
            this.count = count;
            this.pointer = new Pointer(memory, 0);
        }

        internal FlagCollection(int count, byte[] memory, int offset)
        {
            this.count = count;
            this.pointer = new Pointer(memory, offset);
        }

        public string this[int index]
        {
            get
            {
                Pointer ptr = pointer.GetRelativePointer(index * (FlagLength + 1));
                return ptr.String;
            }
            set
            {
                Pointer ptr = pointer.GetRelativePointer(index * (FlagLength + 1));
                ptr.String = value;
            }
        }

        public void Clear(string flag)
        {
            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                Pointer ptr = pointer.GetRelativePointer(offset);
                offset += FlagLength + 1;
                if (ptr.Compare(flag))
                    ptr[0] = 0;
            }
        }

        public bool Contains(string flag)
        {
            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                Pointer ptr = pointer.GetRelativePointer(offset);
                offset += FlagLength;
                if (ptr.Compare(flag))
                    return true;
            }
            return false;
        }

        public int Count
        {
            get { return count; }
        }

        public void Set(string flag)
        {
            Pointer ptr = null;
            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                ptr = pointer.GetRelativePointer(offset);
                offset += FlagLength + 1;
                if (ptr[0] == 0)
                    break;
            }
            ptr.String = flag;
        }
    }
}
